package com.sdp.irrigation.excel.field;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.excel.context.AnalysisContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.irrigation.cache.EmpCache;
import com.sdp.irrigation.db.irrigation_emp.service.IIrrigationEmpService;
import com.sdp.irrigation.entity.IrrigationEmp;
import com.sdp.irrigation.utils.IrrigationConstants;
import com.sdp.right.entity.SysEmp;
import com.sdp.right.entity.SysImportTemplateDetail;
import com.sdp.right.excel.importExcel.ImportExcelCallbackService;
import com.sdp.right.excel.importExcel.ImportExcelRowCallbackService;
import com.sdp.right.service.RightExtService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;

@Service
public class EmpExcelCallback implements ImportExcelCallbackService, ImportExcelRowCallbackService {

	private final ThreadLocal<Set<String>> threadLocal = ThreadLocal.withInitial(HashSet<String>::new);

	@Autowired
	private IIrrigationEmpService empService;
	
	@Autowired
	private RightExtService rightExtService;
	
	@Autowired
	private EmpCache empCache;
	
	@Override
	public BusinessContext execute(HttpServletRequest request, HttpServletResponse response) {
		
		return null;
	}

	@Override
	public <T> void execute(T rowData, List<T> insertList, List<T> updateList,
			List<SysImportTemplateDetail> templateDetailList, AnalysisContext context) {
		IrrigationEmp emp = (IrrigationEmp) rowData;
		
		Set<String> empNoSet = threadLocal.get();
		empNoSet.add(emp.getEmpNo());
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> void executeBatch(List<T> rowDataList, List<T> insertList, List<T> updateList,
			List<SysImportTemplateDetail> templateDetailList, AnalysisContext context) {
		CopyOptions options = CopyOptions.create()
                .setIgnoreNullValue(true);
		Set<String> empNoSet = threadLocal.get();
		QueryWrapper<IrrigationEmp> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().in(IrrigationEmp::getEmpNo, empNoSet);
		List<IrrigationEmp> empList = empService.list(queryWrapper);
		Map<String, IrrigationEmp> empMap = new HashMap<>();
		for (IrrigationEmp emp : empList) {
			empMap.put(emp.getEmpNo(), emp);
		}
		List<SysEmp> sysEmpList = new ArrayList<>();
		
		for (Object obj : rowDataList) {
			IrrigationEmp row = (IrrigationEmp) obj;
			if (empMap.containsKey(row.getEmpNo())) {
				BeanUtil.copyProperties(row, empMap.get(row.getEmpNo()), options);
				updateList.add((T) empMap.get(row.getEmpNo()));
				if (!StringUtils.isEmpty(empMap.get(row.getEmpNo()).getEmpIc())) {
					empCache.deleteCache(empMap.get(row.getEmpNo()).getEmpIc());
				}
				
			} else {
				row.setCreateTime(LocalDateTime.now());
				row.setUpdateTime(row.getCreateTime());
				SysEmp sysEmp = new SysEmp();
				sysEmp.setOrgNo(IrrigationConstants.默认公司编码.getCode());
				BeanUtils.copyProperties(row, sysEmp);
				sysEmpList.add(sysEmp);
				insertList.add((T)row);
			}
		}
		if (sysEmpList.size() > 0) {
			rightExtService.addSysEmpBatch(sysEmpList);
		}
		
		//清理
		sysEmpList.clear();
		sysEmpList = null;
		empList.clear();
		empList = null;
		empMap.clear();
		empMap = null;
		empNoSet.clear();
	}

	

}
